package com.edu.xml.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**
 * @author CharChen
 * @e-mail piggsoft@163.com
 * @since 2013-4-23
 * @version 1.0
 */
@SuppressWarnings("unused")
public class SelectCauseGenerator {
	public static void main(String[] args) throws DocumentException, IOException {
		 SAXReader reader = new SAXReader();
         Document  document = reader.read(new File("src/com/bbs/mapper/xml/PersonMapper.xml"));
         Element root = document.getRootElement();
         Element resultElement = root.element("resultMap");
         parseClass(resultElement);
         
	}
	
	private void addSqlCause(Element root, Document document) throws IOException {
		Element element = root.addElement("sql");
        element.addAttribute("id", "selectCause");
        element.addText("123123");
        writeXml(document, "src/com/bbs/mapper/xml/PersonMapper.xml");
	}
	
	private static void parseClass(Element resultElement) {
		List<Element> elements = resultElement.elements();
		for(Element element : elements) {
			String jdbcType = element.attribute("jdbcType").getStringValue();
			
		}
	}
	
	/** 
     * 输出文件. 
     * @param document 
     * @param filePath 
     * @throws IOException 
     */  
    private static void writeXml(Document document, String filePath) throws IOException  
    {  
        File file = new File(filePath);  
        XMLWriter writer = null;  
        try {  
            if(file.exists())  
            {  
                file.delete();  
            }  
            writer = new XMLWriter(new FileOutputStream(file));  
            writer.write(document);  
            writer.close();  
        } catch (UnsupportedEncodingException e) {  
            e.printStackTrace();  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }finally{  
            if(writer != null)  
            {  
                writer.close();  
            }  
        }  
    }  
}
